package com.dawn.module.hospital.controller.app.base;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cnsu-cmh
 * @date 2019-04-23
 * @description
 */
public class BaseController {
        /**
         * 解析参数
         * @param request
         * @return
         */
        final protected Map<String,Object> parseRequestParamMap(HttpServletRequest request, boolean convertExtArg) {
        Enumeration<String> argEnu = request.getParameterNames();
        Map<String,Object> args=new HashMap();
        while (argEnu.hasMoreElements()) {
            String argName = argEnu.nextElement();
            String[] value = request.getParameterValues(argName);
            if (value == null) {
                continue;
            }
            for (String v : value) {
                args.put(argName,v);
            }
            if(value.length>1){
                args.put(argName,value);
            }
        }
        if(convertExtArg){
            String start=request.getParameter("start");
            if(StringUtils.isNotBlank(start)){
                String limit=request.getParameter("limit");
                int l= NumberUtils.toInt(limit);
                l=l>0?l:20;
                int s=NumberUtils.toInt(start);
                s=s<0?0:s;
                args.put("curPage",s/l + 1);
                args.put("pageSize",l);
            }
        }
        return args;
    }
}
